﻿using System.Collections.Generic;
using System.Configuration;
using BSDS.Commands.Commands;

namespace BSDS.Commands.Configuration
{
    public class MailingConfigurationLoader
    {
        public MailingConfiguration Load()
        {
            var configSection = (MailingConfigurationSection)ConfigurationManager.GetSection("mailing");

            var subjectTemplates = new Dictionary<CommandExitStatus, string>
                                       {
                                           {CommandExitStatus.Success, configSection.SuccessSubjectTemplate},
                                           {CommandExitStatus.Warning, configSection.WarningSubjectTemplate},
                                           {CommandExitStatus.Error, configSection.ErrorSubjectTemplate}
                                       };

            var recipients = new List<string>();
            for (int i = 0; i < configSection.Recipients.Count; i++)
            {
                var recipient = configSection.Recipients[i];
                recipients.Add(recipient.Email);
            }

            var smtpConfiguration = new SmtpConfiguration(configSection.SenderAccount, configSection.SenderPassword,
                                                          configSection.SmtpServer, configSection.SmtpPort, 
                                                          configSection.EnableSsl, configSection.SmtpTimeout);

            return new MailingConfiguration(configSection.NotificationsEnabled, subjectTemplates, recipients, smtpConfiguration);
        }
    }
}